package me.tuyou.springboot.atomikos.web.controller;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {

	@Resource(name = "sysJdbcTemplate")
	private JdbcTemplate sysJdbcTemplate;

	@Resource(name = "busJdbcTemplate")
	private JdbcTemplate busJdbcTemplate;

	@Transactional
	@RequestMapping("/test")
	public String test() {
		System.out.println("begin.....");
		sysJdbcTemplate.execute("insert into sys_a(id) values(1)");
		busJdbcTemplate.execute("insert into bus_b(id) values(2)");
		if (Math.random() > 0.01) {
			throw new RuntimeException();
		}
		System.out.println("end.....");
		return "hello world!";
	}
}
